<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <BreakSpace label="操作记录" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable ref="table" :isSelectColumn="false" height="auto" :columns="columns" :dataSource="tableLists" :onColumnsChange="columns => (this.columns = columns)"> </FilterTable>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button type="primary" v-show="type != 'show'" :click="submitHandler">提 报</multiuse-button>
      <multiuse-button type="primary" v-show="type != 'show'" :click="saveHandler">保 存</multiuse-button>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { getDetailById, getBankName, saveOne, submitData } from '@/api/dealerFinance/FinanceManage/FinanceApply';
import { phoneAndSolidify, telePhone, phone } from '@/utils/validate.js';
import { queryLog } from '@/api/omFinance';

export default {
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'show'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      columns: this.createdTable(),
      formList: this.createdFrom(),
      fetchParams: {},
      fetchApi: () => {},
      formData: {},
      bankList: [],
      tableLists: [],
      infoVisible: false
    };
  },
  mounted() {
    this.init();
  },
  created() {
    //this.getBankNameFun();
  },
  methods: {
    //加载银行名称
    async getBankNameFun() {
      const res = await getBankName();
      if (res.resultCode === 200) {
        this.bankList = res.data.map(x => ({ codeId: x.id, codeCnDesc: x.bankName }));

        res.data.map(x => {
          this.formList.find(x => x.fieldName == 'bankId').itemList.push({ text: x.bankName, value: x.id });
        });
      }
    },
    init() {
      if (this.type != 'add') {
        this.getRecordLog();
        this.getDetailById();
      }
      if (this.type == 'show') {
        this.formList.map(x => {
          x.placeholder = '';
          if (x.rules) {
            x.rules[0].required = false;
          }
        });
        this.formList.find(x => x.fieldName == 'payee').itemList = this.createDictList('6031');
      }
    },
    async getDetailById() {
      const res = await getDetailById(this.rowData.id);
      if (res.resultCode == 200) {
        this.formList.forEach(x => {
          x.initialValue = res.data[x.fieldName];
          if(x.fieldName=='salesCompany'){
            this.isChoose(x.initialValue);
          }
        });
      }
    },
    async getRecordLog() {
      const res = await queryLog(this.rowData.id);
      if (res.resultCode == 200) {
        this.tableLists = res.data || [];
      }
    },
    // 获取最新申请编号

    isChoose(val) {
      if (val == 25551002) {
        this.formList.find(x => x.fieldName == 'payee').rules[0].required = true;
        this.formList.find(x => x.fieldName == 'payee').itemList = this.createDictList('6031',[60311005]);
        this.formList.find(x => x.fieldName == 'payee').initialValue = undefined;
      } else {
        this.formList.find(x => x.fieldName == 'payee').rules[0].required = false;
        this.formList.find(x => x.fieldName == 'payee').itemList = this.createDictList('6031',[60311001,60311002,60311003]);
        this.formList.find(x => x.fieldName == 'payee').initialValue = undefined;
      }
    },
    // 获取表单数据
    formChangeHandle(val) {
      val.bankName = this.transDictText(this.formList.find(x => x.fieldName == 'bankId').initialValue, this.bankList);
      this.saveData = val;
    },
    // 保存
    async saveHandler() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let saveData = Object.assign({}, this.rowData, this.saveData);
        saveData.bankName = this.createDictText(saveData.bankName,6094);
        console.log(saveData, '---保存的数据---');
        let res = {};
        if (this.type === 'add') {
          res = await saveOne(saveData);
        }
        if (this.type === 'edit') {
          res = await saveOne(saveData);
        }
        if (res.resultCode === 200) {
          await this.closeDrawer('refresh');
        }
      }
    },

    // 提报
    async submitHandler() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let saveData = Object.assign({}, this.rowData, this.saveData);
        saveData.bankName = this.createDictText(saveData.bankName,6094);
        console.log(saveData, '---提报的数据---');
        let res = {};
        if (this.type === 'add') {
          res = await submitData(saveData);
        }
        if (this.type === 'edit') {
          res = await submitData(saveData);
        }
        if (res.resultCode === 200) {
          await this.closeDrawer('refresh');
        }
      }
    },
    closeDrawer(type) {
      this.$emit('close', false, type);
    },
    selectChangeHandle() {},

    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '三方融资'
        },
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '银行名称',
          fieldName: 'bankId',
          placeholder: '请选择',
          itemList: this.createDictList('6094'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '出票金额（万）',
          fieldName: 'billAmount',
          min: 0,
          max: 999999999,
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入出票金额', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '出票期限',
          fieldName: 'billDeadline',
          placeholder: '请选择',
          itemList: this.createDictList('6027'),
          rules: [{ required: true, message: '请选择出票期限', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '银行联系人',
          fieldName: 'linkMan',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '银行联系电话',
          fieldName: 'linkPhone',
          placeholder: '请输入',
          rules: [
            {
              validator: phoneAndSolidify,
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'applyDate',
          placeholder: '请选择',
          dateType: 'exactdate',
          minDateTime: moment().add(-1, 'days'),
          initialValue: moment().format('YYYY-MM-DD'),
          rules: [{ required: true, message: '请选择申请日期', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '申请类型',
          fieldName: 'applyType',
          placeholder: '请选择',
          itemList: this.createDictList('6073'),
          rules: [{ required: true, message: '请选择申请类型', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '提车公司',
          fieldName: 'salesCompany',
          placeholder: '请选择',
          itemList: this.createDictList('2555'), // 只能选择“新能源”，等咱们燃油车上线后，再放开限制。
          // itemList: [{ text: '新能源', value: 25551002 }],
          rules: [{ required: true, message: '请选择提车公司', trigger: 'change' }],
          change: val => {
            console.log(val, '---选择提车公司---');
            this.isChoose(val);
          }
        },
        {
          type: 'SELECT',
          label: '收款方',
          fieldName: 'payee',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: false, message: '请选择提收款方', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          selfCols: 3,
          label: '预提车型',
          fieldName: 'reservedModel',
          placeholder: '请输入车型+台数',
          rules: [{ required: true, message: '请输入预提车型', trigger: 'blur' }]
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'applyRemark',
          placeholder: '请输入',
          selfCols: 3,
          maxlength: 200
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作状态',
          dataIndex: 'businessStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6030'),
          render: p => {
            return <span>{this.createDictText(p.row.businessStatus, '6030')}</span>;
          }
        },
        {
          title: '操作人员',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '审核备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
